﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sqlsearcher.Services
{
    public class PrintHelper
    {
        public static string Rep(string org, string source, string target)
        {
            return Rep(org, source, target, 0);
        }
        public static string Rep(string org, string source, string target, int len)
        {
            if (len != 0)
            {
                if (target.Length > len)
                {
                    target = target.Substring(0, len);
                }
            }
            return org.Replace(source, target);
        }
        public static string RepField(string org, string beginTag, string endTag, string block)
        {
            int beginIndex, endIndex;
            string result;

            beginIndex = org.IndexOf(beginTag, 0);
            endIndex = org.IndexOf(endTag, 0);
            if (endIndex <= beginIndex)
            {
                return org;
            }
            else
            {
                result = org.Substring(0, beginIndex) + org.Substring(endIndex);
                result = result.Replace(endTag, block);
                return result;
            }
        }
        public static string GetString(string org, string beginTag, string endTag)
        {
            return GetString(org, beginTag, endTag, "");
        }
        public static string GetString(string org, string beginTag, string endTag, string repBegin)
        {
            int beginIndex, endIndex;
            string result;
            beginIndex = org.IndexOf(beginTag, 0);
            endIndex = org.IndexOf(endTag, 0);
            if (endIndex <= beginIndex)
            {
                return "";
            }
            else
            {
                result = org.Substring(beginIndex, endIndex - beginIndex);
                result = result.Replace(beginTag, repBegin);
                return result;
            }
        }
        public static Tuple<string, bool> GetTemplet(string filePath)
        {

            FileStream aFile = null;
            try
            {
                aFile = new FileStream(@filePath, FileMode.Open, FileAccess.Read);
            }
            catch
            {
                return new Tuple<string, bool>("打开模板文件出错", false);
            }
            StreamReader sr = new StreamReader(aFile, System.Text.Encoding.GetEncoding("gb2312"));
            string templet = sr.ReadToEnd();
            sr.Close();
            return new Tuple<string, bool>(templet, true);
        }

        public static List<string> GetMatchString(string template, string startTag, string endTag)
        {
            var res = new List<string>();
            var str = GetString(template, startTag, endTag);

            res.Add(str);

            var findex = template.IndexOf(str);
            NewMethod(template, startTag, endTag, res, str, findex);

            return res;
        }

        private static void NewMethod(string template, string startTag, string endTag, List<string> res, string str, int findex)
        {
            try
            {
                var str2 = template.Substring(findex + str.Length + endTag.Length, template.Length - findex - str.Length - endTag.Length);
                if (str2.IndexOf(startTag) != -1)
                {
                    var tmpStr = GetString(str2, startTag, endTag);
                    res.Add(tmpStr);

                    var indexTmp = template.IndexOf(tmpStr);
                    NewMethod(template, startTag, endTag, res, tmpStr, indexTmp);
                }
            }
            catch
            {

            }
        }
    }
}
